package com.revenant.driver.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.location.AMapLocation;
import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.nukc.stateview.StateView;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.App;
import com.revenant.driver.R;
import com.revenant.driver.adapter.AreaCitySelectItemAdapter;
import com.revenant.driver.adapter.AreaSelectItemAdapter;
import com.revenant.driver.adapter.AreaSelectItemEndAdapter;
import com.revenant.driver.adapter.Driver.PingTaiAdapter;
import com.revenant.driver.base.BaseFragment;
import com.revenant.driver.bean.BaseBean.MessageEvent;
import com.revenant.driver.bean.Driver.PingTaiBean;
import com.revenant.driver.bean.Selectareabean;
import com.revenant.driver.ui.activity.DriverGoodsDetailActivity;
import com.revenant.driver.ui.activity.LoginActivity;
import com.revenant.driver.ui.activity.NewImproveDriverInfoaActivity;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.LocatinNoDataService;
import com.revenant.driver.utils.LocationService;
import com.revenant.driver.utils.SPUtils;
import com.revenant.driver.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.blankj.utilcode.util.ColorUtils.getColor;
import static com.revenant.driver.bean.BaseBean.MyEventCode.PingTai_Push_Code;
import static com.revenant.driver.bean.Message.Driver_RenZheng_6;
import static com.revenant.driver.bean.Message.Driver_RenZheng_7;
import static com.revenant.driver.bean.Message.Driver_RenZheng_8;
import static com.revenant.driver.utils.Constants.Area_Area;
import static com.revenant.driver.utils.Constants.Area_City;
import static com.revenant.driver.utils.Constants.Area_Provice;
import static com.revenant.driver.utils.Constants.Look_PingTai;

/**
 * @ProjectName: Driver
 * @Package: com.revenant.driver.ui.fragment
 * @ClassName: DriverFragment
 * @Description: java类作用描述
 * @Author: Revenant
 * @CreateDate: 2020-02-07 14:49
 * @UpdateUser: 更新者
 * @UpdateDate: 2020-02-07 14:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DriverPingTaiFragment extends BaseFragment {
    @BindView(R.id.start_place)
    TextView startPlace;
    @BindView(R.id.end_place)
    TextView endPlace;
    @BindView(R.id.jiehuocheliangrecy)
    RecyclerView jiehuocheliangrecy;
    @BindView(R.id.jiehuocheliangsmart)
    SmartRefreshLayout jiehuocheliangsmart;
    @BindView(R.id.loadLayout)
    LinearLayout loadLayout;
    @BindView(R.id.show_select)
    LinearLayout showSelect;
    @BindView(R.id.search_start)
    TextView searchStart;
    @BindView(R.id.search_end)
    TextView searchEnd;
    @BindView(R.id.search_more)
    TextView searchMore;
    @BindView(R.id.show_search)
    LinearLayout showSearch;


    private int pagernum = 1;

    CityPickerView mPickerstart = new CityPickerView();

    CityPickerView mPickerend = new CityPickerView();

    private PingTaiAdapter itemAdapter;


    private String startcode = "0";

    private String endcode = "0";

    private String defaultValue;


    private int priviceselectcount = 0;
    private int cityselectcount = 0;
    private int areaselectcount = 0;

    private int priviceendselectcount = 0;
    private int cityendselectcount = 0;
    private int areaendselectcount = 0;

    private AreaSelectItemAdapter privicestartAdapter;
    private AreaCitySelectItemAdapter citystartAdapter;
    private AreaCitySelectItemAdapter areastartAdapter;


    private AreaSelectItemEndAdapter priviceendAdapter;
    private AreaCitySelectItemAdapter cityendAdapter;
    private AreaCitySelectItemAdapter areaendAdapter;

    private Context context;
    private PopupWindow popstartwindow;
    private PopupWindow popendwindow;
    private PopupWindow popsearch;
    private String startCode = "0";
    private String endCode = "0";
    private String searchinfo = "";

    protected boolean isVisible;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_driver_pingtai;
    }

    @Override
    protected void initView() {
//        defaultValue = App.getApplication().getString(R.string.type_select_default);
//
        if (SPUtils.getAccounId(getActivity()) == 0) {
            showSearch.setVisibility(View.GONE);
        } else {
            showSearch.setVisibility(View.VISIBLE);
        }
//
//
        mStateView = StateView.inject(loadLayout);
//        mPickerstart.init(getActivity());
//        mPickerend.init(getActivity());
        itemAdapter = new PingTaiAdapter();
        jiehuocheliangrecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        jiehuocheliangrecy.setAdapter(itemAdapter);


        jiehuocheliangsmart.autoRefresh();

        initArea();

        mStateView.setOnRetryClickListener(new StateView.OnRetryClickListener() {
            @Override
            public void onRetryClick() {
                //do something
                jiehuocheliangsmart.autoRefresh();
            }
        });
        itemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (SPUtils.getAccounId(getActivity()) == 0) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    App applicationContext = App.getApplication();
                    int userStatus = applicationContext.getUserStatus();
                    switch (userStatus) {
                        case 5:
                            if (itemAdapter.getData() != null) {
                                Bundle bundle = new Bundle();
                                bundle.putInt("goods_type", 0);
                                bundle.putInt("goods_auto", itemAdapter.getData().get(position).isIsAuto()?1:2);
                                bundle.putString("goods_id", String.valueOf(itemAdapter.getData().get(position).getId()));
                                Intent intent = new Intent();
                                intent.putExtras(bundle);
                                intent.setClass(getActivity(), DriverGoodsDetailActivity.class);
                                startActivity(intent);
                            }
                            break;
                        case 6:
                            com.luck.picture.lib.tools.ToastUtils.s(getContext(), Driver_RenZheng_6);
                            Intent intent6 = new Intent();
//                            intent6.setClass(getActivity(), DriverRenZhengOneActivity.class);
                            intent6.setClass(getActivity(), NewImproveDriverInfoaActivity.class);
                            startActivity(intent6);
                            break;
                        case 7:
                            com.luck.picture.lib.tools.ToastUtils.s(getContext(), Driver_RenZheng_7);
                            break;
                        case 8:
                            com.luck.picture.lib.tools.ToastUtils.s(getContext(), Driver_RenZheng_8);
                            Intent intent8 = new Intent();
//                            intent8.setClass(getActivity(), DriverRenZhengOneActivity.class);
                            intent8.setClass(getActivity(), NewImproveDriverInfoaActivity.class);
                            startActivity(intent8);
                            break;
                        default:
                    }
                }

            }
        });
    }

    @Override
    public void initData() {
        jiehuocheliangsmart.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pagernum++;
                searchContent(startCode, endCode, searchinfo);
                jiehuocheliangsmart.finishLoadMore(true);//加载完成
            }
        });
        //刷新
        jiehuocheliangsmart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                itemAdapter.setNewData(null);
                pagernum = 1;
                searchContent(startCode, endCode, searchinfo);
                jiehuocheliangsmart.finishRefresh(true);//刷新完成
            }
        });

        itemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.click_phone:
                        if (itemAdapter.getData().get(position).getPhone()!=null){
                            Utils.callPhone(getActivity(),itemAdapter.getData().get(position).getPhone());
                        }
                        break;
                }
            }
        });


    }

    private void initDataRecyclerview() {
        if (SPUtils.getAccounId(getActivity()) == 0) {
            OkGo.<String>get(Look_PingTai)

                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {

                            PingTaiBean shipperPublishedBean = GsonUtil.parseJsonWithGson(response.body(), PingTaiBean.class);

                            if (shipperPublishedBean.getCode().equals("0")) {
                                int pageNum = shipperPublishedBean.getData().getPageNum();
                                int pages = shipperPublishedBean.getData().getPages();
                                if (pageNum == 1) {
                                    itemAdapter.setNewData(shipperPublishedBean.getData().getResult());
                                } else if (pageNum <= pages) {
                                    itemAdapter.addData(shipperPublishedBean.getData().getResult());
                                } else {
                                    itemAdapter.loadMoreEnd();
                                }

                            } else {
                                itemAdapter.loadMoreFail();
                            }
                            mStateView.showContent();

                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            mStateView.showRetry();
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                        }
                    });

        } else {
            OkGo.<String>get(Look_PingTai)
                    .params("loadingCode", startCode)
                    .params("unloadCode", endCode)
                    .params("keysWord", searchinfo)
                    .params("pageNum", pagernum)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            PingTaiBean shipperPublishedBean = GsonUtil.parseJsonWithGson(response.body(), PingTaiBean.class);

                            if (shipperPublishedBean.getCode().equals("0")) {
                                int pageNum = shipperPublishedBean.getData().getPageNum();
                                int pages = shipperPublishedBean.getData().getPages();
                                if (pageNum == 1) {
                                    itemAdapter.setNewData(shipperPublishedBean.getData().getResult());
                                } else if (pageNum <= pages) {
                                    itemAdapter.addData(shipperPublishedBean.getData().getResult());
                                } else {
                                    itemAdapter.loadMoreEnd();
                                }

                            } else {
                                itemAdapter.loadMoreFail();
                            }

                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            mStateView.showRetry();
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                        }
                    });
        }

    }


    public static DriverPingTaiFragment newInstance(String param1) {
        DriverPingTaiFragment fragment = new DriverPingTaiFragment();
        Bundle args = new Bundle();
        args.putString(param1, param1);
        fragment.setArguments(args);
        return fragment;
    }


    @OnClick({R.id.search_start, R.id.search_end, R.id.search_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_start:
                darkenBackground(0.3f);
                searchStart.setTextColor(getActivity().getColor(R.color.selectareacolor));
                popstartwindow.showAsDropDown(showSearch);
                break;
            case R.id.search_end:
                darkenBackground(0.3f);
                searchEnd.setTextColor(getActivity().getColor(R.color.selectareacolor));
                popendwindow.showAsDropDown(showSearch);
                break;
            case R.id.search_more:
                darkenBackground(0.3f);
                searchMore.setTextColor(getActivity().getColor(R.color.selectareacolor));
                popsearch.showAsDropDown(showSearch);
                break;
        }
    }

    private void alertareastrat() {
        CityConfig cityConfig = new CityConfig.Builder()
                .setCityWheelType(CityConfig.WheelType.PRO_CITY)
                .confirTextColor("#1E90FF")
                .cancelText("全国")
                .provinceCyclic(false)
                .cityCyclic(false)
                .districtCyclic(false)
                .cancelTextColor("#1E90FF").build();
        mPickerstart.setConfig(cityConfig);

        mPickerstart.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                startcode = city.getId();
                startPlace.setText(city.getName());
                jiehuocheliangsmart.autoRefresh();
            }

            @Override
            public void onCancel() {
                ToastUtils.showLongToast(getActivity(), "已取消");
                startcode = "0";
                startPlace.setText(defaultValue);
                jiehuocheliangsmart.autoRefresh();
            }
        });

        mPickerstart.showCityPicker();
    }

    private void alertareaend() {
        CityConfig cityConfig = new CityConfig.Builder()
                .confirTextColor("#1E90FF")
                .cancelText("全国")
                .setCityWheelType(CityConfig.WheelType.PRO_CITY)
                .provinceCyclic(false)
                .cityCyclic(false)
                .districtCyclic(false)
                .cancelTextColor("#1E90FF").build();
        mPickerend.setConfig(cityConfig);

        mPickerend.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                endcode = city.getId();

                endPlace.setText(city.getName());
                jiehuocheliangsmart.autoRefresh();
            }

            @Override
            public void onCancel() {
                ToastUtils.showLongToast(getActivity(), "已取消");
                endcode = "0";
                endPlace.setText(defaultValue);
                jiehuocheliangsmart.autoRefresh();
            }
        });

        mPickerend.showCityPicker();
    }

    @Override
    public void onResume() {
        super.onResume();
//        showLcation();
//        jiehuocheliangsmart.autoRefresh();
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveStickyEvent(MessageEvent event) {
        super.receiveStickyEvent(event);
        switch (event.getCode()) {
            case PingTai_Push_Code:
                jiehuocheliangsmart.autoRefresh();
                break;
        }
    }

    /*无网络*/
    @Override
    public void doNoNetSomething() {
        LogUtils.d("whatnetnonet");
        mStateView.showRetry();
    }

    private void startlocation() {
        Intent serviceIntend = new Intent(getActivity(), LocatinNoDataService.class);
        getActivity().startService(serviceIntend);

    }

    private void showLcation() {
        AMapLocation location = null;
        if (LocationService.getLastLocation() == null) {
            location = LocatinNoDataService.getLastLocation();
        } else {
            location = LocationService.getLastLocation();
        }
        if (location != null) {
            if (startPlace.getText().toString().trim().isEmpty()) {
                startcode = location.getAdCode();
                startPlace.setText(location.getCity());
            } else {

            }

        } else {
            startcode = "0";
            startPlace.setText(defaultValue);
        }
    }

    private void stoplocation() {

        Intent stopIntent = new Intent(getActivity(), LocatinNoDataService.class);
        getActivity().stopService(stopIntent);

    }

    private void initArea() {
        context = getActivity();
        initPopStartWindow();
        initPopEndWindow();
        initPopSearchWindow();


        if (SPUtils.getAccounId(context) != 0) {
            initPrivice(0);
            initPrivice(1);
        }

    }

    private void initPrivice(int type) {
        OkGo.<String>get(Area_Provice)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        if (response.code()==200){
                            if (!response.body().isEmpty()){
                                Selectareabean selectareabean = GsonUtil.parseJsonWithGson(response.body(), Selectareabean.class);
                                if (selectareabean.getCode().equals("0")) {

                                    if (type == 0) {
                                        List<Selectareabean.DataBean> dataStart = selectareabean.getData();
                                        for (int i = 0; i < dataStart.size(); i++) {
                                            dataStart.get(i).setSelectcount(0);
                                        }
                                        privicestartAdapter.setNewData(dataStart);
                                    } else if (type == 1) {
                                        List<Selectareabean.DataBean> dataEnd = selectareabean.getData();
                                        for (int i = 0; i < dataEnd.size(); i++) {
                                            dataEnd.get(i).setSelectcount(1);
                                        }
                                        priviceendAdapter.setNewData(dataEnd);
                                    }


                                }
                            }
                        }


                    }
                });
    }

    /*type=0 起始地,type=1 目的地*/

    private void initCity(String provicecode, int type) {
        OkGo.<String>get(Area_City)
                .params("provinceCode", provicecode)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Selectareabean selectareabean = GsonUtil.parseJsonWithGson(response.body(), Selectareabean.class);
                        if (selectareabean.getCode().equals("0")) {
                            if (type == 0) {
                                citystartAdapter.setNewData(selectareabean.getData());
                            } else if (type == 1) {
                                cityendAdapter.setNewData(selectareabean.getData());
                            }
                        }

                    }
                });
    }

    private void initArea(String citycode, int type) {
        OkGo.<String>get(Area_Area)
                .params("cityCode", citycode)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Selectareabean selectareabean = GsonUtil.parseJsonWithGson(response.body(), Selectareabean.class);
                        if (selectareabean.getCode().equals("0")) {
                            if (type == 0) {
                                areastartAdapter.setNewData(selectareabean.getData());
                            } else if (type == 1) {
                                areaendAdapter.setNewData(selectareabean.getData());
                            }
                        }

                    }
                });
    }


    private void initPopStartWindow() {
        View contentView = View.inflate(context, R.layout.alertselectwindow, null);
        RecyclerView scrollProvice = (RecyclerView) contentView.findViewById(R.id.scroll_provice_start);
        RecyclerView scrollCity = (RecyclerView) contentView.findViewById(R.id.scroll_city_start);
        RecyclerView scrollArea = (RecyclerView) contentView.findViewById(R.id.scroll_area_start);
        TextView cancle = (TextView) contentView.findViewById(R.id.search_clear);
        TextView ok = (TextView) contentView.findViewById(R.id.search_confirm);


        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                for (int i = 0; i < privicestartAdapter.getData().size(); i++) {
                    if (privicestartAdapter.getData().get(i).getSelect()) {
                        privicestartAdapter.getData().get(i).setSelect(false);
                        privicestartAdapter.notifyItemChanged(i);
                    }
                }
                citystartAdapter.setNewData(null);
                areastartAdapter.setNewData(null);

                priviceselectcount = 0;
                cityselectcount = 0;
                areaselectcount = 0;

                startCode = "0";
                pagernum = 1;
                jiehuocheliangsmart.autoRefresh();
//                searchContent(startCode, endCode, searchinfo);
                searchStart.setTextColor(getActivity().getColor(R.color.bottom_normal_text));
                popstartwindow.dismiss();

            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if ((priviceselectcount + cityselectcount + areaselectcount) == 0) {
                    startCode = "0";
                } else {

                    if (areaselectcount >= cityselectcount && cityselectcount > 0) {
                        for (int i = 0, j = 0; i < areastartAdapter.getData().size(); i++) {
                            if (areastartAdapter.getData().get(i).getSelect() == true) {
                                if (j == 0) {
                                    startCode = areastartAdapter.getData().get(i).getCode();
                                    j++;
                                } else {
                                    startCode += "," + areastartAdapter.getData().get(i).getCode();
                                }
                            }
                        }
                    } else if (cityselectcount >= priviceselectcount && priviceselectcount > 0) {
                        for (int i = 0, j = 0; i < citystartAdapter.getData().size(); i++) {
                            if (citystartAdapter.getData().get(i).getSelect() == true) {
                                if (j == 0) {
                                    startCode = citystartAdapter.getData().get(i).getCode();
                                    j++;
                                } else {
                                    startCode += "," + citystartAdapter.getData().get(i).getCode();
                                }
                            }
                        }
                    } else {
                        for (int i = 0, j = 0; i < privicestartAdapter.getData().size(); i++) {
                            if (privicestartAdapter.getData().get(i).getSelect() == true) {
                                if (j == 0) {
                                    startCode = privicestartAdapter.getData().get(i).getCode();
                                    j++;
                                } else {
                                    startCode += "," + privicestartAdapter.getData().get(i).getCode();
                                }
                            }
                        }
                    }

                }
                pagernum = 1;
                jiehuocheliangsmart.autoRefresh();
//                searchContent(startCode, endCode, searchinfo);
                popstartwindow.dismiss();

            }
        });


        privicestartAdapter = new AreaSelectItemAdapter();
        citystartAdapter = new AreaCitySelectItemAdapter();
        areastartAdapter = new AreaCitySelectItemAdapter();


        scrollProvice.setLayoutManager(new LinearLayoutManager(getActivity()));
        scrollProvice.setAdapter(privicestartAdapter);

        scrollCity.setLayoutManager(new LinearLayoutManager(getActivity()));
        scrollCity.setAdapter(citystartAdapter);


        scrollArea.setLayoutManager(new LinearLayoutManager(getActivity()));
        scrollArea.setAdapter(areastartAdapter);


        privicestartAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


                if (privicestartAdapter.getData().get(0).getSelectcount() == 0) {
                    boolean select = privicestartAdapter.getData().get(position).getSelect();

                    if (select == false) {
                        priviceselectcount++;
                        privicestartAdapter.getData().get(position).setSelect(!select);
                        privicestartAdapter.notifyItemChanged(position);
                    } else {
                        priviceselectcount--;
                        privicestartAdapter.getData().get(position).setSelect(!select);
                        privicestartAdapter.notifyItemChanged(position);
                    }

                    if (priviceselectcount == 1) {
                        cityselectcount = 0;
                        for (int i = 0; i < privicestartAdapter.getData().size(); i++) {
                            if (privicestartAdapter.getData().get(i).getSelect()) {
                                initCity(privicestartAdapter.getData().get(i).getCode(), 0);
                            }
                        }
                    } else {
                        cityselectcount = 0;
                        areaselectcount = 0;

                        citystartAdapter.setNewData(null);
                        citystartAdapter.notifyDataSetChanged();

                        areastartAdapter.setNewData(null);
                        areastartAdapter.notifyDataSetChanged();
                    }
                }


            }
        });


        citystartAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                boolean select = citystartAdapter.getData().get(position).getSelect();

                if (select == false) {
                    cityselectcount++;
                    citystartAdapter.getData().get(position).setSelect(!select);
                    citystartAdapter.notifyItemChanged(position);
                } else {
                    cityselectcount--;
                    citystartAdapter.getData().get(position).setSelect(!select);
                    citystartAdapter.notifyItemChanged(position);
                }

                if (cityselectcount == 1) {
                    areaselectcount = 0;
                    for (int i = 0; i < citystartAdapter.getData().size(); i++) {
                        if (citystartAdapter.getData().get(i).getSelect()) {
                            initArea(citystartAdapter.getData().get(i).getCode(), 0);
                        }
                    }

                } else {
                    areaselectcount = 0;
                    areastartAdapter.setNewData(null);
                    areastartAdapter.notifyDataSetChanged();
                }


            }
        });


        areastartAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                boolean select = areastartAdapter.getData().get(position).getSelect();
                if (select == false) {
                    areaselectcount++;
                    areastartAdapter.getData().get(position).setSelect(!select);
                    areastartAdapter.notifyItemChanged(position);
                } else {
                    areaselectcount--;
                    areastartAdapter.getData().get(position).setSelect(!select);
                    areastartAdapter.notifyItemChanged(position);
                }


            }
        });

        popstartwindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true
        );


        // 设置PopupWindow是否能响应外部点击事件
        popstartwindow.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        popstartwindow.setTouchable(true);
        popstartwindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        popstartwindow.setAnimationStyle(R.style.Popupwindow);
        popstartwindow.setContentView(contentView);

        popstartwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
                if (priviceselectcount + cityselectcount + areaselectcount == 0) {
                    searchStart.setTextColor(getActivity().getColor(R.color.bottom_normal_text));
                }
            }
        });

//        darkenBackground(0.3f);

    }

    private void initPopEndWindow() {
        View contentView = View.inflate(context, R.layout.alertselectendwindow, null);
        RecyclerView scrollProvice = (RecyclerView) contentView.findViewById(R.id.scroll_provice);
        RecyclerView scrollCity = (RecyclerView) contentView.findViewById(R.id.scroll_city);
        RecyclerView scrollArea = (RecyclerView) contentView.findViewById(R.id.scroll_area);
        TextView cancle = (TextView) contentView.findViewById(R.id.search_clear);
        TextView ok = (TextView) contentView.findViewById(R.id.search_confirm);


        popendwindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true
        );


        // 设置PopupWindow是否能响应外部点击事件
        popendwindow.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        popendwindow.setTouchable(true);
        popendwindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popendwindow.setAnimationStyle(R.style.Popupwindow);

        popendwindow.setContentView(contentView);

        popendwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);

                if (priviceendselectcount + cityendselectcount + areaendselectcount == 0) {
                    searchEnd.setTextColor(getActivity().getColor(R.color.bottom_normal_text));
                }

            }
        });

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                for (int i = 0; i < priviceendAdapter.getData().size(); i++) {
                    if (priviceendAdapter.getData().get(i).getSelect()) {
                        priviceendAdapter.getData().get(i).setSelect(false);
                        priviceendAdapter.notifyItemChanged(i);
                    }
                }
                cityendAdapter.setNewData(null);
                areaendAdapter.setNewData(null);

                priviceendselectcount = 0;
                cityendselectcount = 0;
                areaendselectcount = 0;

                endCode = "0";
                pagernum = 1;
                jiehuocheliangsmart.autoRefresh();
//                searchContent(startCode, endCode, searchinfo);
                searchEnd.setTextColor(getActivity().getColor(R.color.bottom_normal_text));
                popendwindow.dismiss();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if ((priviceendselectcount + cityendselectcount + areaendselectcount) == 0) {
                    endCode = "0";
                } else {

                    if (areaendselectcount >= cityendselectcount && cityendselectcount > 0) {
                        for (int i = 0, j = 0; i < areaendAdapter.getData().size(); i++) {
                            if (areaendAdapter.getData().get(i).getSelect() == true) {
                                if (j == 0) {
                                    endCode = areaendAdapter.getData().get(i).getCode();
                                    j++;
                                } else {
                                    endCode += "," + areaendAdapter.getData().get(i).getCode();
                                }
                            }
                        }
                    } else if (cityendselectcount >= priviceendselectcount && priviceendselectcount > 0) {
                        for (int i = 0, j = 0; i < cityendAdapter.getData().size(); i++) {
                            if (cityendAdapter.getData().get(i).getSelect() == true) {
                                if (j == 0) {
                                    endCode = cityendAdapter.getData().get(i).getCode();
                                    j++;
                                } else {
                                    endCode += "," + cityendAdapter.getData().get(i).getCode();
                                }
                            }
                        }
                    } else {
                        for (int i = 0, j = 0; i < priviceendAdapter.getData().size(); i++) {
                            if (priviceendAdapter.getData().get(i).getSelect() == true) {
                                if (j == 0) {
                                    endCode = priviceendAdapter.getData().get(i).getCode();
                                    j++;
                                } else {
                                    endCode += "," + priviceendAdapter.getData().get(i).getCode();
                                }
                            }
                        }
                    }

                }
                pagernum = 1;
                jiehuocheliangsmart.autoRefresh();
//                searchContent(startCode, endCode, searchinfo);
                popendwindow.dismiss();

            }
        });


        priviceendAdapter = new AreaSelectItemEndAdapter();
        cityendAdapter = new AreaCitySelectItemAdapter();
        areaendAdapter = new AreaCitySelectItemAdapter();


        scrollProvice.setLayoutManager(new LinearLayoutManager(getActivity()));
        scrollProvice.setAdapter(priviceendAdapter);

        scrollCity.setLayoutManager(new LinearLayoutManager(getActivity()));
        scrollCity.setAdapter(cityendAdapter);


        scrollArea.setLayoutManager(new LinearLayoutManager(getActivity()));
        scrollArea.setAdapter(areaendAdapter);


        priviceendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


                if (priviceendAdapter.getData().get(0).getSelectcount() == 1) {
                    boolean select = priviceendAdapter.getData().get(position).getSelect();

                    if (select == false) {
                        priviceendselectcount++;
                        priviceendAdapter.getData().get(position).setSelect(!select);
                        priviceendAdapter.notifyItemChanged(position);
                    } else {
                        priviceendselectcount--;
                        priviceendAdapter.getData().get(position).setSelect(!select);
                        priviceendAdapter.notifyItemChanged(position);
                    }

                    if (priviceendselectcount == 1) {
                        cityendselectcount = 0;
                        for (int i = 0; i < priviceendAdapter.getData().size(); i++) {
                            if (priviceendAdapter.getData().get(i).getSelect()) {
                                initCity(priviceendAdapter.getData().get(i).getCode(), 1);
                            }
                        }
                    } else {
                        cityendselectcount = 0;
                        areaendselectcount = 0;

                        cityendAdapter.setNewData(null);
                        cityendAdapter.notifyDataSetChanged();

                        areaendAdapter.setNewData(null);
                        areaendAdapter.notifyDataSetChanged();
                    }
                }


            }
        });


        cityendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                boolean select = cityendAdapter.getData().get(position).getSelect();

                if (select == false) {
                    cityendselectcount++;
                    cityendAdapter.getData().get(position).setSelect(!select);
                    cityendAdapter.notifyItemChanged(position);
                } else {
                    cityendselectcount--;
                    cityendAdapter.getData().get(position).setSelect(!select);
                    cityendAdapter.notifyItemChanged(position);
                }

                if (cityendselectcount == 1) {
                    areaendselectcount = 0;
                    for (int i = 0; i < cityendAdapter.getData().size(); i++) {
                        if (cityendAdapter.getData().get(i).getSelect()) {
                            initArea(cityendAdapter.getData().get(i).getCode(), 1);
                        }
                    }

                } else {
                    areaendselectcount = 0;
                    areaendAdapter.setNewData(null);
                    areaendAdapter.notifyDataSetChanged();
                }


            }
        });


        areaendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                boolean select = areaendAdapter.getData().get(position).getSelect();
                if (select == false) {
                    areaendselectcount++;
                    areaendAdapter.getData().get(position).setSelect(!select);
                    areaendAdapter.notifyItemChanged(position);
                } else {
                    areaendselectcount--;
                    areaendAdapter.getData().get(position).setSelect(!select);
                    areaendAdapter.notifyItemChanged(position);
                }


            }
        });

//        darkenBackground(0.3f);


    }

    private void initPopSearchWindow(
    ) {
        View contentView = View.inflate(getActivity(), R.layout.alert_find_something, null);
        EditText searchedit = (EditText) contentView.findViewById(R.id.find_search);

        TextView cancle = (TextView) contentView.findViewById(R.id.seach_clear);
        TextView ok = (TextView) contentView.findViewById(R.id.search_confirm);

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (searchedit.length() > 0) {
                    searchedit.setText("");
                    searchinfo = "";
                }else {
                    searchinfo="";
                }
                pagernum = 1;
                jiehuocheliangsmart.autoRefresh();
//                searchContent(startCode, endCode, searchinfo);

                searchMore.setTextColor(getActivity().getColor(R.color.bottom_normal_text));

                popsearch.dismiss();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchedit.length() > 0) {
                    searchinfo = searchedit.getText().toString().trim();
                    pagernum = 1;
//                    searchContent(startCode, endCode, searchinfo);
                    jiehuocheliangsmart.autoRefresh();
                    popsearch.dismiss();
                } else {
                    Toast.makeText(getActivity(), "请输入搜索内容", Toast.LENGTH_SHORT).show();
                }

            }
        });


        popsearch = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true
        );


        // 设置PopupWindow是否能响应外部点击事件
        popsearch.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        popsearch.setTouchable(true);

        popsearch.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        popsearch.setAnimationStyle(R.style.Popupwindow);

        popsearch.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (searchedit.length() < 1) {
                    searchMore.setTextColor(getActivity().getColor(R.color.bottom_normal_text));
                }
                darkenBackground(1f);
            }
        });

        popsearch.setContentView(contentView);
//        darkenBackground(0.3f);


    }


    private void darkenBackground(Float alpha) {
        if (alpha < 0 || alpha > 1) {
            return;
        }
        WindowManager.LayoutParams windowLP = getActivity().getWindow().getAttributes();
        windowLP.alpha = alpha;
        if (alpha == 1) {
            //不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            //此行代码主要是解决在华为手机上半透明效果无效的bug
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        getActivity().getWindow().setAttributes(windowLP);

    }

    private void searchContent(String startCode, String endCode, String searchinfo) {
        LogUtils.d("aaaaa:" + "开始的:" + startCode + "," + "结束的:" + endCode + "," + searchinfo);

        initDataRecyclerview();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        super.onDestroy();
        if (popstartwindow != null) {
            popstartwindow.dismiss();
        }

        if (popendwindow != null) {
            popendwindow.dismiss();
        }

        if (popsearch != null) {
            popsearch.dismiss();
        }
    }
}
